<?php

namespace Admin\Controller;
use Think\Controller;

class GameAdvertisersController extends Controller{
	
	/*
	新增游戏管理
	*/

	public function add(){
		$this->isAjaxLogin();//检查是否登录

		$game_name=I('post.gameName');//帐号
		$ad_id=I('post.selOpt');//所属广告主id
		$layered=I('post.layerName');//分层比列，
		$game_link=I('post.gameLink');//游戏链接
		$game_status=I('post.radioValueStatus');//1表示正常，0表示停封
		$is_recommend=I('post.radioValueIf');//是否推荐，1表示推荐，0表示不推荐
        $remarks=I('post.remarkName');//备注
        $gameintroduction=I('post.gameintroduction');//游戏简介
        $popularity=I('post.popularity');//人气
        $details=I('post.details');//游戏图片包含详情
        $gamepicture=I('post.gamepicture');//游戏图片
		$gametext=I('post.gametext');//游戏详情
        $presult=M('game_advertisers');
        $presentid=$presult->field('id')->order('id desc')->find();

        $zfc=implode(',', $presentid)+1;





        $data['game_name']=$game_name;
        $data['ad_id']=$ad_id;
        $data['layered']=$layered;
        $data['game_link']=$game_link;
        $data['game_status']=$game_status;
        $data['is_recommend']=$is_recommend;
        $data['remarks']=$remarks;
        $data['gameintroduction']=$gameintroduction;
        $data['popularity']=$popularity;
        $data['details']=$details;
        $data['gamepicture']=$gamepicture;
        $data['gametext']=$gametext;
        $data['present']=$zfc;
        $data['insert_time']=time();
        $data['status'] = 1;

		$model=D('game_advertisers');

        if($model->create($data) && $model->add($data)){
        	
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        $this->ajaxReturn($ajaxReturn);

	}

	/*
	展示游戏管理
	*/
	public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page      = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');

        $ssre=M('advertisers');

        $count = $ssre
                ->alias('a')
                ->field('a.corporate_name,b.*')
                ->order("b.id desc")
                ->limit($limitStart,$page_size)
                ->join('table_game_advertisers  b ON  b.ad_id=a.id')
                ->where("b.status=1")
                ->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $ssre=M('advertisers');
        $result=$ssre
                ->alias('a')
                ->field('a.corporate_name,b.*')
                ->order("b.id desc")
                ->limit($limitStart,$page_size)
                ->join('table_game_advertisers  b ON  b.ad_id=a.id')
                ->where("b.status=1")
                ->select();


        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

    /*展示
    游戏所属广告主
    */
   

    //修改获取用户信息
    public  function infos(){
        $this->isAjaxLogin();

        $id = I('post.id/d');

        $Model = M('game_advertisers');

        $result = $Model->where([
                'id'=>$id,
            ])->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }



    public function cid_lists(){
        $this->isAjaxLogin();//检查是否登录

        $model=M('advertisers');

        $result=$model->distinct(true)->where("status=1")->select();

         if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }


    /*
    根据广告主id选出对应的游戏
    */

    public function cid_alllists(){
        $this->isAjaxLogin();//检查是否登录
        $page = I('post.page/d');//当前页数
        $page_size = I('post.page_size/d');
        $cid=I('post.user_id');
        $model=M('advertisers');

        $where = 'b.status=1';

        if($cid != ''){
                $where.=" and b.ad_id=$cid";
        }
        $count = $model->alias('a')->join('table_game_advertisers  b ON  b.ad_id=a.id')->where($where)->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;



        $result=$model->alias('a')->field('a.corporate_name,b.*')->order("b.id desc")->join('table_game_advertisers  b ON  b.ad_id=a.id')->where($where)->limit($limitStart,$page_size)->select();
         if($result !== false ){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
    }

	/*
	删除游戏管理
	*/

	public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $ids = I('get.id');

        $model = M('game_advertisers');

        $ids = explode(',',$ids);
        $data['status']=0;

        foreach($ids as $id){
            $result=$model-> where('id='.$id)->save($data);
        }
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }

    /*
    修改游戏
    */
    public function update(){
    	$this->isAjaxLogin();//检查是否登录

    	$model=D('game_advertisers');
		$game_name=I('post.gameName');//帐号
		$ad_id=I('post.selOpt');//所属广告主id
		$layered=I('post.layerName');//分层比列，
		$game_link=I('post.gameLink');//游戏链接
		$game_status=I('post.radioValueStatus');//1表示正常，0表示停封
		$is_recommend=I('post.radioValueIf');//是否推荐，1表示推荐，0表示不推荐
		$remarks=I('post.remarkName');//备注
        $gameintroduction=I('post.gameintroduction');//游戏简介
        $popularity=I('post.popularity');//人气
        $details=I('post.details');//游戏图片包含详情
        $gamepicture=I('post.gamepicture');//游戏图片
        $gametext=I('post.gametext');//游戏详情
        $presult=M('game_advertisers');
        $presentid=$presult->field('id')->order('id desc')->find();

        $zfc=implode(',', $presentid)+1;


		$id=I('post.id');
        $data['game_name']=$game_name;
        $data['layered']=$layered;
        $data['game_link']=$game_link;
        $data['game_status']=$game_status;
        $data['is_recommend']=$is_recommend;
        $data['remarks']=$remarks;
         $data['gameintroduction']=$gameintroduction;
        $data['popularity']=$popularity;
        $data['details']=$details;
        $data['gamepicture']=$gamepicture;
        $data['gametext']=$gametext;
        $data['present']=$zfc;
        $data['insert_time']=time();
        if($model->create($data) &&$model->where('id='.$id)->save($data)){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  $model->getError();
        }
        
        $this->ajaxReturn($ajaxReturn);
    }
}              